// index.js
// 获取应用实例
import Toast from '../../ui/toast/index'
const app = getApp()

Page({
  data: {
    pic_url:app.pic_url,
    swiperList:[],
    cate_list:[],//分类
    us:[],//关于我们
    new_list:[],//最新
    tj_list:[],//推荐
    list:[],//列表
    page:{
      page:1,
    },
    enable: false,
    loading:false,
    loadingProps: {
      size: '50rpx',
    },
  },

  //获取轮播
  get_flash(){
    app.http.request("index.php?r=api/index/flash").then((res)=>{
      console.log(res);
      if(res.data.result){
        let data = res.data.data;
        for(let i=0;i<data.length;i++){
          this.data.swiperList.push(data[i].thumb_pic);
        }
        this.setData({
          swiperList:this.data.swiperList,
        });
      }
    });
  },

  //下拉
  onRefresh() {
    this.setData({ enable: true });
    setTimeout(() => {
      this.setData({ enable: false });
      this.get_list();
    }, 1000);
  },

  //获取分类
  get_cate_list(){
    app.http.request("index.php?r=api/index/indexcatelist").then((res)=>{
      if(res.data.result){
        this.setData({
          cate_list:res.data.data,
        });
      }
    });
  },

  //获取关于
  get_us(){
    app.http.request("index.php?r=api/index/indexus",{param:{id:1}}).then((res)=>{
      if(res.data.result){
        this.setData({
          us:res.data.data,
        });
      }
    });
  },

  //获取最新产品
  get_new_list(){
    app.http.request("index.php?r=api/index/newlist",{param:{num:5,type:1}}).then((res)=>{
      if(res.data.result){
        this.setData({
          new_list:res.data.data,
        });
      }
    });
  },

  //获取推荐产品
  get_tj_list(){
    app.http.request("index.php?r=api/index/newlist",{param:{num:5,type:2}}).then((res)=>{
      if(res.data.result){
        this.setData({
          tj_list:res.data.data,
        });
      }
    });
  },

  //获取列表
  get_list(){
    this.setData({
      loading:true,
    });
    app.http.request("index.php?r=api/index/goodslist",{page:this.data.page}).then((res)=>{
      this.setData({
        loading:false,
      });
      if(res.data.result==1){
        this.data.list = this.data.list.concat(res.data.data);
        this.setData({
            list:this.data.list,
        });
        this.data.page.page++;
      }else{
        Toast({
            selector:"#toast",
            message:res.data.data,
            context:this,
        });
      }
    });
  },
  
  //跳转
  to_url(e){
    let type = e.currentTarget.dataset.type;
    let url = "/pages/index/index";
    switch(type){
      case "cate":
        url = "/pages/cate/index";
        break;
      case "home":
        break;  
      case "news":
        url="/pages/news/list/index";
        break;
      case "us":
        url="/pages/us/index";
        break;    
    }
    wx.redirectTo({
      url: url,
    })
  },

  //跳转片段
  to_block(e){
    let id = e.currentTarget.dataset.mb;
    wx.navigateTo({
      url: '/pages/block/index?id='+id,
    })
  },

  //跳转内容
  to_content(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/content/index?id='+id,
    })
  },

  //跳转列表
  to_list(e){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/list/index?id='+id,
    })
  },
 

  onLoad() {
    this.get_flash();
    this.get_cate_list();
    this.get_us();
    this.get_new_list();
    this.get_tj_list();
    this.get_list();
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('web_name'),
    })
  },
  
  /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
      return {
          title:  wx.getStorageSync('web_name'),
          imageUrl: this.data.us.thumb_pic,
          path: '/pages/index/index', 
        }
    },

    onShareTimeline(){
      return {
        title:  wx.getStorageSync('web_name'),
        imageUrl: this.data.us.thumb_pic,
        path: '/pages/index/index', 
      }
  },

})
